<?php
namespace app\forum\controller;
use app\forum\controller\UserController;
/**
 * 帖子回复
 */

class ReplyController extends UserController {

    /**
     * 回复帖子
     */
    public function index()
    {
        $threadId = request('get.thread_id',0,'intval');
        if (empty($threadId)) {
            $this->error404();
        }
        $model = target('forum/Thread');
        //获取内容信息
        if(!empty($threadId)){
            $threadInfo = $model->getInfo($threadId);
        }else{
            $this->error404();
        }
        //信息判断
        if (!is_array($threadInfo)){
            $this->error404();
        }
        $plateInfo = target('forum/Plate')->getInfo($threadInfo['plate_id']);
        //检测权限
        $this->checkForumPurview($threadInfo['plate_id'],'view,reply');
        if (!$threadInfo['status']){
            $this->error('该主题已经关闭，无法进行回复操作！');
        }
        if($threadInfo['lock']){
            $this->error('该主题已锁定，您无法回复！');
        }
        //验证token
        if(!target('forum/Thread')->validToken('reply_'.$threadId,request('post.token'))){
            $this->error('抱歉，系统安全验证失败，请刷新后重新提交！');
        }
        $_POST['thread_id'] = $threadId;
        if(target('forum/Reply')->saveData('add','content')){
            //增加积分
            if($plateInfo['integral_reply']){
                target('forum/Integral')->settingInt($this->loginUserInfo['username'],$plateInfo['integral_add'],1,'回帖奖励');
            }
            $this->success('帖子回复成功！',url($threadInfo['app'].'/Thread/index',array('thread_id'=>$threadId)));
        }else{
            $msg = target('forum/Reply')->getError();
            if(empty($msg)){
                $this->error('帖子回复失败');
            }else{
                $this->error($msg);
            }
        }
    }
}